package com.qingzhu.controller.orderserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojo.ShopCart;
import com.qingzhu.pojovo.OrderVo;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.OrderService;
import com.qingzhu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/order/add")
public class OrderInsertController extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    OrderService orderService = new OrderServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("订单创建服务..");

        String readJson = req.getReader().readLine();
        OrderVo orderVo = objectMapper.readValue(readJson, OrderVo.class);
        ShopCart shopCart = orderVo.getShopCart();
        String id = orderVo.getId();
        orderVo = orderService.createOrder(shopCart, id);
        ResultVo resultVo = orderVo.getResult()>0 ? new ResultVo(200,"订单创建成功！",orderVo):
                new ResultVo(500,"订单创建失败！");
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
